package com.forevershine.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUtils {
	
	private static SimpleDateFormat sdFormat = new SimpleDateFormat(
	"yyyyMMddHHmmss");
	
	// convert test.jpg to test_yyyyMMddHHmmss.jpg, to avoid upload-covering
	public static String appendTimeStampToFileName(String baseFileName) {
		String result = baseFileName;
		String random = sdFormat.format(new Date());
		if (baseFileName.lastIndexOf(".") > 0) {
			int pos = baseFileName.lastIndexOf(".");
			result = baseFileName.substring(0, pos) + "_" + random
					+ baseFileName.substring(pos);
		}
		return result;
	}
	
	/**
	 * copy from spring 
	 * @param str
	 * @return
	 */
	public static boolean hasLength(String str) {
		return (str != null && str.length() > 0);
	}

	/**
	 * copy from spring 
	 * Check whether the given String has actual text.
	 * More specifically, returns <code>true if the string not null,
	 * its length is greater than 0, and it contains at least one non-whitespace character.
	 * <p>

	 * StringUtils.hasText(null) = false
	 * StringUtils.hasText("") = false
	 * StringUtils.hasText(" ") = false
	 * StringUtils.hasText("12345") = true
	 * StringUtils.hasText(" 12345 ") = true
	 * </pre>
	 * @param str the String to check (may be <code>null)
	 * @return <code>true if the String is not null, its length is
	 * greater than 0, and it does not contain whitespace only
	 * @see java.lang.Character#isWhitespace
	 */
	public static boolean hasText(String str) {
		if (!hasLength(str)) {
			return false;
		}
		int strLen = str.length();
		for (int i = 0; i < strLen; i++) {
			if (!Character.isWhitespace(str.charAt(i))) {
				return true;
			}
		}
		return false;
	}

}
